#include "cd_lock.h"
#include "be_atomic.h"

CD_NAMESPACE_BEGIN

c_lock::c_lock():para(0)
{

}

c_lock::~c_lock(){

}

s32	c_lock::lock(s32	par){
	if(para	==	par){
		++cnt;
		return	0;
	}
	while (be::atomic_compare_exchange32(
		(volatile	long*)&para,	par,	0)	!=	0);
	return	-1;
}

s32	c_lock::unlock(s32	par){
	if(para	!=	par){
		return	-1;
	}
	if(!cnt){
		be::atomic_exchange32((volatile	long*)&para,	0);
	}else{
		--cnt;
	}

	return	0;
}

CD_NAMESPACE_END